import ShopTable from '@/components/admin/shop-table';
import ErrorComp from '@/components/common/error-comp';
import LeftToRightView from '@/components/common/motion/left-to-right-view';
import { shopStatusValues } from '@/lib/db/schema/enum';
import {
  getJsonToEnumArraySchema,
  jsonToDateRangeSchema,
  pageSchema,
} from '@/service/query/schema/common';
import { adminShopSearchSchema } from '@/service/query/schema/shop';
import { adminListShopsByPage } from '@/service/query/shop';
import type { SearchParams } from '@/service/type/common';

export const metadata = {
  title: '商铺管理',
};

const PageSchema = pageSchema
  .merge(adminShopSearchSchema)
  .omit({ dateRange: true, status: true })
  .extend({
    dateRange: jsonToDateRangeSchema,
    status: getJsonToEnumArraySchema(shopStatusValues),
  });

export default async function Page({
  searchParams,
}: {
  searchParams: SearchParams;
}) {
  const params = PageSchema.safeParse(await searchParams);
  if (!params.success) return <ErrorComp message="参数错误" />;

  const res = adminListShopsByPage({
    page: params.data.page,
    pageSize: params.data.pageSize,
    id: params.data.id ? +params.data.id : undefined,
    name: params.data.name,
    address: params.data.address,
    contactPhone: params.data.contactPhone,
    status: params.data.status,
    dateRange: params.data.dateRange,
  });

  return (
    <LeftToRightView>
      <div className="p-5 rounded-lg bg-background space-y-8">
        <div className="text-2xl font-bold">商铺管理</div>
        <ShopTable pageData={res} />
      </div>
    </LeftToRightView>
  );
}
